﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace MyTask.Infrastructure
{
   public class SendEmail
    {
        /// <summary>

        /// Send mail

        /// </summary>

        /// <param name="smtpserver">SMTP Server</param>

        /// <param name="enableSsl">Whether to enable SSL encryption</param>

        /// <param name="userName">log in account</param>

        /// <param name="pwd">password</param>

        /// <param name="nickName">Sender nickname</param>

        /// <param name="fromEmail">Sender</param>

        /// <param name="toEmail">Recipient</param>

        /// <param name="subj">Topic</param>

        /// <param name="bodys">Content</param>

        public static async Task SendMailAsync(string smtpserver, bool enableSsl, string userName, string pwd, string nickName, string fromMail, string toMail, string subj, string bodys)

        {

            SmtpClient smtpClient = new SmtpClient();

            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            smtpClient.Host = smtpserver;

            smtpClient.Credentials = new NetworkCredential(userName, pwd);

            smtpClient.EnableSsl = enableSsl;

            MailAddress fromAddress = new MailAddress(fromMail, nickName);

            MailAddress toAddress = new MailAddress(toMail);

            MailMessage mailMessage = new MailMessage(fromAddress, toAddress);

            mailMessage.Subject = subj;

            mailMessage.Body = bodys;

            mailMessage.BodyEncoding = Encoding.Default;

            mailMessage.IsBodyHtml = true;

            mailMessage.Priority = MailPriority.Normal;

            await smtpClient.SendMailAsync(mailMessage);

        }
    }
}
